package com.remoteapp.activities;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.remoteapp.config.AppConfig;
import com.remoteapp.main.R;

public class ViewActivity extends Activity implements OnItemSelectedListener,
		OnClickListener, TextWatcher, OnCheckedChangeListener {

	/** Spinner to select GPIO */
	Spinner spinnerGPIOs;

	/** EditText to change name of current GPIO */
	EditText editName;

	/** EditText to change name of current GPIO */
	CheckBox checkInvert;

	/** Button to save current value */
	Button buttonSave;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view);

		spinnerGPIOs = (Spinner) findViewById(R.id.spinnerGPIOS);
		editName = (EditText) findViewById(R.id.editName);
		buttonSave = (Button) findViewById(R.id.buttonSave);
		checkInvert = (CheckBox) findViewById(R.id.checkInvert);

		spinnerGPIOs.setOnItemSelectedListener(this);
		editName.addTextChangedListener(this);
		buttonSave.setOnClickListener(this);
		checkInvert.setOnCheckedChangeListener(this);

		refreshSpinner();
	}

	@Override
	protected void onDestroy() {
		try {
			Activity act = AppConfig.getMainActivity();
			if (act != null) {
				((MainActivity) act).refreshSwitches();
			}

			AppConfig.saveConfig();
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), e.getMessage(),
					Toast.LENGTH_LONG).show();
		}
		super.onDestroy();
	}

	/**
	 * Refreshes the spinner content
	 */
	private void refreshSpinner() {
		// Set adapter for spinner
		ArrayAdapter<String> list = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item);

		for (int i = 0; i < AppConfig.GPIO_NAMES.length; i++) {
			list.add(AppConfig.GPIO_NAMES[i] + " ("
					+ String.format(Locale.getDefault(), "%02d", i) + ")");
		}

		list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerGPIOs.setAdapter(list);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.view, menu);
		return true;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
			long arg3) {
		editName.setEnabled(true);
		editName.setText(AppConfig.GPIO_NAMES[pos]);
		buttonSave.setEnabled(false);
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		editName.setEnabled(false);
		buttonSave.setEnabled(false);
	}

	@Override
	public void onClick(View v) {
		if (v == buttonSave) {
			int pos = spinnerGPIOs.getSelectedItemPosition();
			AppConfig.GPIO_NAMES[pos] = editName.getText().toString();
			AppConfig.INVERT_GPIOS[pos] = checkInvert.isChecked();

			buttonSave.setEnabled(false);
			refreshSpinner();
			spinnerGPIOs.setSelection(pos);
		}
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		buttonSave.setEnabled(true);
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		buttonSave.setEnabled(true);
	}

}
